package fy.form.listen.log;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import fy.config.DBConfigBean;
import fy.form.listen.dao.LogDao;

public class LogReader {

	private List<Long> summaryIdList = new ArrayList<Long>();

	private String result;

	public void read(String logFilePath) {
		try {
			BufferedReader br = fy.util.MyUtil
					.createBufferedReader(logFilePath);

			String temp = null;

			while ((temp = br.readLine()) != null
					&& !"".equals((temp = temp.replaceAll(" ", "")))) {

				long summaryId = Long.valueOf(temp);

				summaryIdList.add(summaryId);
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void findFormInfo(DBConfigBean bean) {
		if (summaryIdList.size() > 0) {
			LogDao ld = new LogDao(bean.getDriverClassName(), bean.getUrl(),
					bean.getUser(), bean.getPassword());
			List<JSONObject> jsonList = ld.findLogInfo(summaryIdList);
			StringBuilder sb = new StringBuilder().append("[");
			for (int i = 0, size = jsonList.size(); i < size; i++) {
				if (i != 0) {
					sb.append(",");
				}
				sb.append(jsonList.get(i).toString());
			}
			sb.append("]");
			result = sb.toString();
			ld = null;
		} else {
			result = "[]";
		}
	}

	public String getLogJSON() {
		summaryIdList = null;
		return result;
	}
}
